/**
 * This Class provides all the methods neccessary to authenticate,
 * edit, update and delete user access credentials.
 */
package server;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 *
 * @author Muhammad Asad Ullah
*/

public class DBFunctions {
     /**
      * Data base Credentials.
      */
       private String uri ;
       private String username ;
       private String password ;
      //End Data base Credentials.
      /**
       *
       * @param URI - Data base URI
       * @param UserName - Data base access user name
       * @param PassWord - Data base access password
       */
    public DBFunctions(String URI,String UserName,String PassWord)
    {
        this.uri = URI;
        this.username = UserName;
        this.password = PassWord;
        try
        {
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
       Connection con = null;
        Boolean valid = false;
        con = DriverManager.getConnection(uri,username,password);
        CallableStatement iproc = con.prepareCall("{call authenticate(?,?,?)}");
        iproc.setString(1, "admin");
        iproc.setString(2, "admin");
        iproc.setString(3, "true");
        Boolean b = iproc.execute();
        if(b == true || b == false)
        {
            System.out.println("Data base is accessable");
        }
        }

        
        catch(Exception e)
        {
            System.out.println("Can not load data base drivers or Data base is not accessable");
            e.printStackTrace();
            System.exit(1);
        }
    }
    /**
     *  To Add a new User (Client)
     * @param userName for new User
     * @param Password for new User
     * @param superuser  true if super user else false (as Strings)
     * @return true if Operation is successful and false other wise.
     */
    public Boolean addUser(String userName,String Password,String superuser)
    {
        Connection con = null;
        try
        {
        con = DriverManager.getConnection(uri,username,password);
        CallableStatement iproc = con.prepareCall("{call Adduser(?,?,?)}");
        iproc.setString(1, userName);
        iproc.setString(2, Password);
        iproc.setString(3, superuser.toString());
        iproc.execute();
        int i = 0; i = iproc.getUpdateCount();
        try{con.close();}catch(Exception g){g.printStackTrace();}
        if(i == 1)return true; else return false;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            try{con.close();}catch(Exception e){e.printStackTrace();}
            return false;
        }

    }
    /**
     *  To Edit user's (client's) access credentials and security role.
     * @param Username   Current User Name
     * @param newUsername New User Name
     * @param newPassword New Password
     * @param superUser   true if Superuser and false other wise (as Strings)
     * @return true if Operation is successful and false other wise.
     */
    public Boolean editUser(String Username,String newUsername,String newPassword,String superUser)
    {
        Connection con = null;
        try
        {
        con = DriverManager.getConnection(uri,username,password);
        CallableStatement iproc = con.prepareCall("{call updateinfo(?,?,?,?)}");
        iproc.setString(1, Username);
        iproc.setString(2, newUsername);
        iproc.setString(3, newPassword);
        iproc.setString(4, superUser);
        Boolean b = iproc.execute();

        int i = 0; i = iproc.getUpdateCount(); 
        try{con.close();}catch(Exception g){g.printStackTrace();}
        if(i == 1)return true;
        else
            return false;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            try{con.close();}catch(Exception e){e.printStackTrace();}
            return false;
        }
    }
    /**
     * To Authenticate if provided credentials belong to a valid user
     * @param Username
     * @param Password
     * @param superUser
     * @return Boolean if authentication is successful and false otherwise.
     */
    public Boolean Authenticate(String Username,String Password,String superUser)
    {
        Connection con = null;
        Boolean valid = false;
        try
        {
        con = DriverManager.getConnection(uri,username,password);        
        CallableStatement iproc = con.prepareCall("{call authenticate(?,?,?)}");
        iproc.setString(1, Username);
        iproc.setString(2, Password);
        iproc.setString(3, superUser);
        Boolean b = iproc.execute(); 
        if(iproc.getUpdateCount() == 0)
        valid = false;
        else valid = true;
        
      }
        catch(Exception ex)
        {ex.printStackTrace();try{con.close();}catch(Exception g){g.printStackTrace();return valid;} return valid; }
        return valid;
    }
    /**
     * To delete a user (client)
     * @param userName of User to be deleted
     * @return true upon successful deletion and false other wise.
     */
    public Boolean deleteUser(String userName)
    {
        Connection con = null;
        try
        {
        con = DriverManager.getConnection(uri,username,password);
        CallableStatement iproc = con.prepareCall("{call deleteUser(?)}");
        iproc.setString(1, userName);
        iproc.execute();
        int i = 0; i = iproc.getUpdateCount();
        try{con.close();}catch(Exception g){g.printStackTrace();}
        if(i == 1)return true; else return false;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            try{con.close();}catch(Exception e){e.printStackTrace();}
            return false;
        }     
    }
   
}
